#!/usr/bin/env python
# _*_coding:utf-8 _*_
__author__ = "Gao"
'''飞船沿着星球绕圈'''

import random,math,pygame
import sys

from pygame.locals import *


def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))

def wrap_angle(angle):
    return angle % 360

class Point(object):
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __str__(self):
        return "{X:" + "{:.0f}".format(self.x) +",Y:" + "{:.0f}".format(self.y) + "}"

pygame.init()
screen = pygame.display.set_mode((800,600))
font = pygame.font.Font(None,18)

# 背景图
space = pygame.image.load('space.jpg').convert()

# 行星
planet = pygame.image.load('planet.png').convert_alpha()
planet_width,planet_height = planet.get_size()

# 飞船
ship = pygame.image.load('ship.tga').convert_alpha()
ship_width,ship_height = ship.get_size()
ship = pygame.transform.smoothscale(ship,(ship_width//2,ship_height//2))


radius = 250    # 飞船移动的半径
angle = 0.0    # 飞船移动的角度
pos = Point(0,0)
old_pos = Point(0,0)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    screen.blit(space,(0,0))    # 背景图

    screen.blit(planet,(400-planet_width/2,300-planet_height/2))    # 行星

    # 移动飞船
    angle = wrap_angle(angle + 0.1)
    pos.x = math.cos(math.radians(angle)) * radius
    pos.y = math.sin(math.radians(angle)) * radius

    # 计算飞船的旋转角度
    delta_x = (pos.x - old_pos.x)
    delta_y = (pos.y - old_pos.y)
    rangle = math.atan2(delta_y,delta_x)
    rangled = wrap_angle(-math.degrees(rangle))

    scratch_ship = pygame.transform.rotate(ship , rangled)    # 旋转飞船到指定角度

    width,height = scratch_ship.get_size()    # 获取飞船旋转后的宽度和高度

    x = 400+pos.x - width//2
    y = 300+pos.y-height//2
    screen.blit(scratch_ship,(x,y))

    pygame.display.update()
    old_pos.x = pos.x
    old_pos.y = pos.y





